package com.witmore.neutron.ai.web.apiv2.stock;

import com.jk.common.response.ResponsePageResult;
import com.jk.common.response.ResponseResult;
import com.witmore.neutron.ai.web.wechat.controller.ScorpionBaseController;
import com.witmore.neutron.ai.web.wechat.dto.StockAbnormalRecordDTO;
import com.witmore.neutron.ai.web.wechat.dto.StockAbnormalRecordQueryDTO;
import com.witmore.neutron.ai.web.wechat.service.StockAbnormalRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * V2 股票异动记录控制器
 * Author: 豆子高
 * Date: 2025/12/19
 */
@Slf4j
@RestController
@RequestMapping("/v2/stock-abnormal-record")
@Tag(name = "V2 股票异动记录管理", description = "股票异动记录的查询和详情接口")
public class StockAbnormalRecordControllerV2 extends ScorpionBaseController {

    @Autowired
    private StockAbnormalRecordService stockAbnormalRecordService;

    /**
     * 分页查询股票异动记录
     *
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/query")
    @Operation(summary = "查询股票异动记录", description = "分页查询股票异动记录列表，支持按股票代码、异动类型、日期等条件筛选")
    public ResponsePageResult<StockAbnormalRecordDTO> queryAbnormalRecords(@RequestBody StockAbnormalRecordQueryDTO queryDTO) {
        super.validateUserLogin();
        log.info("查询股票异动记录请求: {}", queryDTO);
        try {
            return stockAbnormalRecordService.queryAbnormalRecords(queryDTO);
        } catch (Exception e) {
            log.error("查询股票异动记录异常", e);
            return ResponsePageResult.fail(500, "查询失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取股票异动记录详情
     *
     * @param id 记录ID
     * @return 记录详情
     */
    @GetMapping("/get")
    @Operation(summary = "获取异动记录详情", description = "根据ID获取指定的股票异动记录详情")
    public ResponseResult<StockAbnormalRecordDTO> getAbnormalRecord(@RequestParam("id") Long id) {
        log.info("获取股票异动记录详情请求, ID: {}", id);
        try {
            return stockAbnormalRecordService.getAbnormalRecordById(id);
        } catch (Exception e) {
            log.error("获取股票异动记录详情异常", e);
            return ResponseResult.fail(500, "获取失败：" + e.getMessage());
        }
    }

    /**
     * 根据股票代码查询异动记录
     *
     * @param stockCode 股票代码
     * @param queryDTO  分页参数
     * @return 分页结果
     */
    @GetMapping("/by-stock-code")
    @Operation(summary = "按股票代码查询异动记录", description = "根据股票代码查询对应的异动记录列表")
    public ResponsePageResult<StockAbnormalRecordDTO> getAbnormalRecordsByStockCode(
            @RequestParam String stockCode,
            StockAbnormalRecordQueryDTO queryDTO) {

        log.info("根据股票代码查询异动记录请求: stockCode={}, page={}, size={}", stockCode, queryDTO.getPage(), queryDTO.getSize());
        super.validateUserLogin();
        try {
            // 设置股票代码
            queryDTO.setStockCode(stockCode);
            return stockAbnormalRecordService.queryAbnormalRecords(queryDTO);
        } catch (Exception e) {
            log.error("根据股票代码查询异动记录异常", e);
            return ResponsePageResult.fail(500, "查询失败：" + e.getMessage());
        }
    }
}
